package elibrary;
import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.sql.*;
import java.util.*;
import javax.servlet.ServletOutputStream;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.digester.Digester;


/**
 * Servlet implementation class JasperReportServelet
 */
public class JasperReportServelet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public JasperReportServelet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String type=request.getParameter("type");
		JasperReport jasperReport = null;
		JasperPrint jasperPrint = null;
		JasperDesign jasperDesign = null;
		ServletOutputStream servletOutputStream = response.getOutputStream();
		String qry;
		
		try
		{	String path = getServletContext().getRealPath("/config.jsp");
			Config conf = new Config();
			conf.readConfig(path);	
			String connectURL = conf.connectURL.substring(1, conf.connectURL.length()-2);
			String DBuser = conf.DBuser.substring(1, conf.DBuser.length()-2);
			String DBpass = conf.DBpass.substring(1, conf.DBpass.length()-2);
			// create a map of parameters to pass to the report.
			Map parameters = new HashMap();
			// get database connection
			Class.forName("com.ibm.db2.jcc.DB2Driver");
			Connection conn = DriverManager.getConnection(connectURL, DBuser, DBpass);
			if(type.equals("user")){
				parameters.put("Report_Title", "List of Users");
				qry="SELECT * FROM USERS";
				parameters.put("query", qry);
				jasperDesign = JRXmlLoader.load(getServletContext().getRealPath("/report/user.jrxml"));
				jasperReport = JasperCompileManager.compileReport(jasperDesign);
				jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);
				 
				JasperExportManager.exportReportToPdfFile(jasperPrint, getServletContext().getRealPath("/report/user.pdf"));
			}
			if(type.equals("book")){
				parameters.put("Report_Title", "List of Books");
				qry="SELECT * FROM BOOK";
				parameters.put("query", qry);
				jasperDesign = JRXmlLoader.load(getServletContext().getRealPath("/report/book.jrxml"));
				jasperReport = JasperCompileManager.compileReport(jasperDesign);
				jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);
				 
				JasperExportManager.exportReportToPdfFile(jasperPrint, getServletContext().getRealPath("/report/book.pdf"));
			}
			
			response.setContentType("application/pdf");
			JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
		 
			servletOutputStream.flush();
			servletOutputStream.close();
		}
		catch(SQLException sqle)
		{
			System.err.println(sqle.getMessage());
		}
		catch (ClassNotFoundException e)
		{
			System.err.println("No such class found!");
		}
		catch (JRException e)
		{
			// display stack trace in the browser
			StringWriter stringWriter = new StringWriter();
			PrintWriter printWriter = new PrintWriter(stringWriter);
			e.printStackTrace(printWriter);
			response.setContentType("text/plain");
			response.getOutputStream().print(stringWriter.toString());
		}
		catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
